package  
{
	import emb.SoundLib;
	import enemies.Turret;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Transform;
	import guns.*
	import flash.ui.Keyboard;
	import Main;
	import ex.KeyboardEx;
	/**
	 * ...
	 * @author Daria Savkova
	 */
	public class Player extends MovingCircle
	{
		private var leftArrow:Boolean;
		private var rightArrow:Boolean;
		private var upArrow:Boolean;
		private var downArrow:Boolean;
		public var zButton:Boolean;
		public var xButton:Boolean;
		public var cButton:Boolean;
		public var fire:Boolean;
		public var wantEnter:Boolean;
		public var wantEject:Boolean;
		public var gun:Gun;
		public const MAXARMOR:int = 1000;
		public const MAXHP:int = 1000;
		public var posesses:Follower;
		
		public var KeyForward:uint;
		public var KeyBackward:uint;
		public var KeyLeft:uint;
		public var KeyRight:uint;
		public var KeyAction:uint;
		public var KeyBoost:uint;
		
		public var KeyEnter:uint;
		public var KeyEject:uint;
		
		public var KeyMute:uint;
		
		public var KeyPause:uint;
		public var KeyMenu:uint;
		
		
		
		
		
		public var AmmoMines:int;
		
		public function Player() 
		{
			//WASD + Z + X
			KeyAction = KeyboardEx.Z;
			KeyForward = KeyboardEx.W;
			KeyBackward = KeyboardEx.S;
			KeyLeft = KeyboardEx.A;
			KeyRight = KeyboardEx.D;
			KeyBoost = KeyboardEx.X;
			//Arrows + SPACE
			KeyAction = Keyboard.SPACE;
			KeyForward = Keyboard.UP;
			KeyBackward = Keyboard.DOWN;
			KeyLeft = Keyboard.LEFT;
			KeyRight = Keyboard.RIGHT;
			KeyBoost = Keyboard.SHIFT;
			//
			KeyEnter = KeyboardEx.Q;
			KeyEject = KeyboardEx.E;
			
			KeyMute = KeyboardEx.M;
			
			KeyPause = KeyboardEx.P;
			KeyMenu = Keyboard.ESCAPE;
			
			
			AmmoMines = 0;
			posesses = null;
			var stage:Stage = Main.Instance().stage;
			hitpoints = MAXHP;
			super(5, 5.0); // r=15
			shield = 0.0;
			hitpoints = MAXHP;
			armor = MAXARMOR;
			gun = new Gun(this);
			gun.BPS = 7;
			gun.FPS = 30;
			gun.spread = 10;
			//stage.addEventListener(Event.ENTER_FRAME, movePlayer);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
			MaskPublic = GroupPlayer;
			MaskCollide = GroupPlayer | GroupEnemy | GroupWall;
			Draw();
		}
		override protected function Draw():void
		{
			graphics.beginFill(IsAlive() ? 0xFF0000 : 0xFF7007);
			graphics.drawCircle(0, 0, r); //
//			graphics.drawRect(-50, -50, 100, 100);
			graphics.beginFill(IsAlive() ? 0xFF6666 : 0xFF0F0F);
			graphics.drawCircle(0, 0, r/2);
			graphics.drawRect(-r/5, -r/5, r, 2*r/5);
			graphics.endFill();
			if (!IsAlive())
				Main.Instance().killPlayer();
		}
		public function keyPressed(event: KeyboardEvent, down:Boolean):void
		{
			var o:MovingCircle = posesses;
			if (!posesses) o = this;
			trace(event.keyCode)
			
			switch (event.keyCode)
			{
				
			case KeyLeft:
				leftArrow = down;
				break;
			case KeyForward:
				upArrow = down;
				break;
			case KeyRight:
				rightArrow = down;
				break;
			case KeyBackward:
				downArrow = down;
				break;
			case KeyAction:
				fire = down;
				break;
			case KeyBoost:
				o.boost = down;
				break;
			case KeyEnter:
				wantEnter = down;
				break;
			case KeyEject:
				wantEject = down;
				break;
			case KeyMute:
				//TO DO: Add hud indicator/control
				if (down)
					SoundLib.Muted = !SoundLib.Muted;
				break;
			case KeyPause:
				if (down)
					Main.Instance().PauseKey();
				break;
			}
			if (o as Turret && posesses)
				(o as Turret).gun.triggerHappy = fire;
			else if (o === this)
			{
				gun.triggerHappy = fire;
			}
			
			if (posesses && wantEject)
				UnPosess(posesses);
		
			//Main.instance.killFollower();
			//o.boost = event.shiftKey;
		}
		
		public function keyPressedDown(event:KeyboardEvent):void
		{
			keyPressed(event, true);
		}
		
		public function keyPressedUp(event:KeyboardEvent):void
		{
			keyPressed(event, false);
		}
		override public function movePlayer(event:Event):void
		{
			if (posesses && !posesses.IsAlive())
			{
				UnPosess(posesses);
			}
			
			var o:MovingCircle = posesses;
			if (!posesses) o = this;
			if (leftArrow)
				o.rotation -= 5;
			if (rightArrow)
				o.rotation += 5;
			if (upArrow)
				o.moveForward();
			else if (downArrow)
				moveBackward();
			else o.dx = o.dy = 0;
			if (posesses)
			{
				x = o.x;
				y = o.y;
			}
			if (this.gun)
				gun.think(null);
		}
		
		public function Posess(f:Follower)
		{
			posesses = f;
			f.brainDead = true;
			f.ColorMePosessed();
			this.MaskPublic = 0;
			this.MaskInteract = 0;
			this.MaskCollide = 0;
			f.MaskPublic = GroupPlayer;	
		}
		
		public function UnPosess(f:Follower)
		{
			posesses = null;
			f.MaskCollide = GroupEnemy;
			f.dx = 0 ;
			f.dy = 0;
			if (f as Turret)
				(f as Turret).gun.triggerHappy = false;
			MaskCollide = GroupEnemy | GroupWall;
			MaskInteract = 0;
			MaskPublic = GroupPlayer;
		}
		
		public function moveBackward():void
		{
			var speed:Number = 3.0;
			var angle:Number = Math.PI * rotation / 180;
			var m:Matrix = new Matrix();
			m.rotate(angle);
			var p:Point = new Point(-speed, 0);
			p = m.transformPoint(p);
			dx = p.x;
			dy = p.y;
		}
		override public function destroy():void
		{
			//Main.Instance().stage.removeEventListener(Event.ENTER_FRAME, movePlayer);
			Main.Instance().stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
			Main.Instance().stage.removeEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
		//	Main.Instance().killPlayer();
		}
		override public function Damage(dam:Number)
		{
			var a:Boolean = IsAlive();
			if (!armor)
			{
				trace("armor zero");
				hitpoints -= Math.max(0.0, dam - shield);
			}
			else
			{
				trace(armor);
				armor -= Math.max(0.0, dam - shield);
				if (armor < 0)
				{
					trace("armor<0!")
					hitpoints += armor;
					armor = 0;
				}
			}
			if (a != IsAlive())
			{
				Draw();
				OnDie();
				Main.Instance().killPlayer();
			}
			Main.Instance().updateHpBar();
			Main.Instance().updateArmorBar();
		}
	}

}